package com.xrj4j.k_tally.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.xrj4j.k_tally.model.BillItem
import com.xrj4j.k_tally.model.BillItemDao

@Database(entities = [BillItem::class], version = 1, exportSchema = true)
abstract class AppDatabase : RoomDatabase() {

    abstract fun getBillDao(): BillItemDao
    companion object {
        private const val dbName = "k_tally.db"

        @Volatile
        private var instance: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase {
            return instance ?: synchronized(this) {
                instance ?: buildDatabase(context).also { instance = it }
            }
        }

        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(context, AppDatabase::class.java, dbName)
                .addCallback(InitDbCall(context))
                .build()
        }
    }

}

class InitDbCall(private val context: Context) : RoomDatabase.Callback() {
    override fun onCreate(db: SupportSQLiteDatabase) {

    }
}